package bb.lanxing.lib.widget;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.appcompat.widget.AppCompatImageView;

//import android.widget.ImageView;

public class CardImageView extends AppCompatImageView {
    public static final int SCALE_TYPE_BASE_WIDTH = 1;
    public static final int SCALE_TYPE_SQUARE = 2;
    private int baseType;

    public CardImageView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        this.baseType = SCALE_TYPE_SQUARE;
    }

    public CardImageView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        this.baseType = SCALE_TYPE_SQUARE;
    }

    public CardImageView(Context context) {
        super(context);
        this.baseType = SCALE_TYPE_SQUARE;
    }

    @Override
    protected void onMeasure(int widthMesureSpec, int heightMesureSpec) {
        Drawable drawable = getDrawable();
        if (drawable != null) {
            int size = View.MeasureSpec.getSize(widthMesureSpec);
            setMeasuredDimension(size, this.baseType == SCALE_TYPE_BASE_WIDTH ? (drawable.getIntrinsicHeight() * size) / drawable.getIntrinsicWidth() : size);
            return;
        }
        super.onMeasure(widthMesureSpec, heightMesureSpec);
    }

    public int getBaseType() {
        return this.baseType;
    }

    public void setScaleType(int i) {
        this.baseType = i;
    }
}
